package com.enniswu.weitter.user.update;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BasePhotoActivity;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.util.ImageUtil;
import com.enniswu.weitter.widget.PopupPicsSelector;
import com.enniswu.weitter.widget.TextEditView;
import com.jph.takephoto.model.TResult;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdateInfoActivity extends BasePhotoActivity<UpdateInfoContract.Presenter> implements UpdateInfoContract.View, PopupPicsSelector.OnPickListener {

    @BindView(R.id.iv_portrait)
    ImageView ivPortrait;
    @BindView(R.id.iv_female)
    ImageView ivFemale;
    @BindView(R.id.iv_male)
    ImageView ivMale;
    @BindView(R.id.tev_name)
    TextEditView tevName;
    @BindView(R.id.tev_synopsis)
    TextEditView tevSynopsis;
    private PopupPicsSelector pop;
    private int userId;
    private boolean isFirst;
    private File fPortrait;
    private File mPortrait;
    private File portrait;
    private int gender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_info);
        setTitle(R.string.info_update);
        pop = new PopupPicsSelector(this, this);
        User user = getIntent().getParcelableExtra("user");
        fPortrait = new File(ImageUtil.getPath(this, ImageUtil.PORTRAIT_FEMALE));
        mPortrait = new File(ImageUtil.getPath(this, ImageUtil.PORTRAIT_MALE));
        if (user != null) {
            userId = user.getId();
            Glide.with(this).load(user.getPortrait())
                    .apply(new RequestOptions().circleCrop()).into(ivPortrait);
            tevName.setText(user.getName());
            tevSynopsis.setText(user.getSynopsis());
            gender = user.getGender();Log.d("用户", user.toString());
        } else {
            userId = getIntent().getIntExtra("userId", 0);
            isFirst = true;
        }
        setGenderIcon();
        presenter = new UpdateInfoPresenter(this);
    }

    @Override
    public void next() {
        finish();
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(ImageUtil.getCropUri(this), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromGalleryWithCrop(ImageUtil.getCropUri(this), getCropOptions());
    }

    @Override
    public void takeSuccess(TResult result) {
        portrait = new File(result.getImage().getOriginalPath());
        Glide.with(this).load(portrait).apply(new RequestOptions().circleCrop()).into(ivPortrait);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        showMessage(msg);
    }

    @OnClick({R.id.iv_portrait, R.id.iv_female, R.id.iv_male, R.id.fab_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_portrait:
                pop.show(view);
                break;
            case R.id.iv_female:
                gender = 0;
                setGenderIcon();
                break;
            case R.id.iv_male:
                gender = 1;
                setGenderIcon();
                break;
            case R.id.fab_ok:
                presenter.update(userId, portrait, tevName.getText().toString(),
                        gender, tevSynopsis.getText().toString());
                break;
            default:
                break;
        }
    }

    private void setGenderIcon() {
        if (gender == 0) {
            ivFemale.setImageResource(R.drawable.ic_female);
            ivMale.setImageResource(R.drawable.ic_male_uncheck);
            if (isFirst) {
                portrait = fPortrait;
                Glide.with(this).load(portrait)
                        .apply(new RequestOptions().circleCrop()).into(ivPortrait);
            }
        } else {
            ivFemale.setImageResource(R.drawable.ic_female_uncheck);
            ivMale.setImageResource(R.drawable.ic_male);
            if (isFirst) {
                portrait = mPortrait;
                Glide.with(this).load(portrait)
                        .apply(new RequestOptions().circleCrop()).into(ivPortrait);
            }
        }
    }
}
